package NC62_判断是不是平衡二叉树;


class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;

}

public class Solution {
    public boolean IsBalanced_Solution(TreeNode root){
        if (root == null){
            return true;
        }
        int left = TreeDepth(root.left);
        int right = TreeDepth(root.right);
        int res = left - right;

        if (res > 1 || res < -1){
            return false;
        }

        return IsBalanced_Solution(root.left) && IsBalanced_Solution(root.right);
    }

    private int TreeDepth(TreeNode root) {
        if (root == null){
            return 0;
        }
        int nLeft = TreeDepth(root.left);
        int nRight = TreeDepth(root.right);
        return Math.max(nLeft, nRight) + 1;
    }
}
